package com.foxmail.aroundme.boring.weight;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.foxmail.aroundme.boring.BuildConfig;
import com.socks.library.KLog;

/**
 */

public class GWebView extends WebView {
    private boolean isDestroy = false;
    private long last_time = 0;

    public GWebView(Context context) {
        super(context);
        isDestroy = false;
    }

    public GWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        isDestroy = false;
    }

    public GWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        isDestroy = false;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public GWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        isDestroy = false;
    }


    @Override
    public void destroy() {
        isDestroy = true;
        ViewGroup parent = (ViewGroup) this.getParent();
        if (parent != null) {
            parent.removeView(this);
        }
        this.removeAllViews();
        super.destroy();
    }

    @Override
    public void loadUrl(String url) {
        if (BuildConfig.DEBUG) {
            KLog.i("状态=" + isDestroy);
        }
        if (isDestroy) {
            return;
        }
        super.loadUrl(url);
    }

    @Override
    public void removeJavascriptInterface(String name) {
        if (isDestroy) {
            return;
        }
        super.removeJavascriptInterface(name);
    }

    //屏蔽双击放大功能
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                long current_time = System.currentTimeMillis();
                long d_time = current_time - last_time;
                if (d_time < 300) {
                    last_time = current_time;
                    return true;
                } else {
                    last_time = current_time;
                }
                break;
        }
        return super.onTouchEvent(event);
    }
}
